package com.beetlestech.nfe.main.biz.com.impl;

import java.util.ArrayList;
import java.util.List;

import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.fw.core.util.StringUtil;
import com.beetlestech.nfe.main.biz.com.HbVTeacherList;
import com.beetlestech.nfe.main.biz.com.dto.VteacherinfolistInfoDTO;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.db.base.StatementDTO;
import com.beetlestech.nfe.main.db.base.StatementUtil;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.tbl.TeachercourseDTO;
import com.beetlestech.nfe.main.db.view.VteacherinfolistDTO;
public class HbVTeacherListImpl extends AbstractDBLogic implements HbVTeacherList, CtgCodeConstant, HbMessageCode, CommonConstant, SQLConstant {

	@Override
	public VteacherinfolistInfoDTO[] getTeacherList(
			String userName,
			String trueName,
			Integer courseTypePId,
			Integer courseTypeCId
			) {
		List<VteacherinfolistInfoDTO> teacherList = new ArrayList<VteacherinfolistInfoDTO>();
		List<VteacherinfolistDTO> teacherListDB = new ArrayList<VteacherinfolistDTO>();
		VteacherinfolistDTO vteacherinfolistDTO =new VteacherinfolistDTO();
		StringBuilder sb=new StringBuilder("teacher_id in (");
		StatementDTO statementDTO = new StatementDTO();
		if(StringUtil.isNotEmpty(userName)){
			StatementUtil.andWhere(statementDTO, VteacherinfolistDTO.COLUMN_USERNAME, StatementUtil.KEYWORD.LIKE ,userName);
		}
		if(StringUtil.isNotEmpty(trueName)){
			StatementUtil.andWhere(statementDTO, VteacherinfolistDTO.COLUMN_NAME, StatementUtil.KEYWORD.LIKE ,trueName);
		}
		if(courseTypePId!=0){
			TeachercourseDTO teachercourseDTO =new TeachercourseDTO();
			//List<Integer> teacherIdArray=new ArrayList<Integer>();
			teachercourseDTO.setcoursetypep(courseTypePId);
			if(courseTypeCId!=0){
				teachercourseDTO.setcoursetypec(courseTypeCId);
			}
			List<TeachercourseDTO> teachercourseDTOArreay=queryDAO.queryForObjectList(TeachercourseDTO.EQUAL_SEARCH, teachercourseDTO);
			if(teachercourseDTOArreay!=null){
				for (int i=0;i<teachercourseDTOArreay.size();i++) {
					if(i!=teachercourseDTOArreay.size()-1){
						sb.append(teachercourseDTOArreay.get(i).getteacherid().toString()+",");
					}
					else{
						sb.append(teachercourseDTOArreay.get(i).getteacherid().toString());
					}
					
				}
				sb.append(")");
			}
		}
		if(!sb.toString().equals("teacher_id in (")){
			if(statementDTO.getSQLStatement()!=null){
				vteacherinfolistDTO.set_dynamicWhereCondition(statementDTO.getSQLStatement()+"and "+sb.toString());
			}else{
				vteacherinfolistDTO.set_dynamicWhereCondition(sb.toString());
			}
			
		}else{
			vteacherinfolistDTO.set_dynamicWhereCondition(statementDTO.getSQLStatement());
		}
		
		teacherListDB=queryDAO.queryForObjectList(VteacherinfolistDTO.SQL_SEARCH, vteacherinfolistDTO);
		if(teacherListDB==null || teacherListDB.size()==0){
			createApplicationException("errMsg", "没找到要查询的信息");
		}else{
			teacherList=convertToVteacherListInfo(teacherListDB);
		}
		return teacherList.toArray(new VteacherinfolistInfoDTO[teacherList.size()]);
		
	}
	private List<VteacherinfolistInfoDTO> convertToVteacherListInfo(List<VteacherinfolistDTO> itemArray){
		List<VteacherinfolistInfoDTO> vteacherinfolistInfoDTOArray = new ArrayList<VteacherinfolistInfoDTO>();
		int a=-1;
		VteacherinfolistInfoDTO temp = null;
		for(VteacherinfolistDTO item:itemArray){
			if(item.getteacherid()==a){
				temp.setcatagorycname(temp.getcatagorycname()+","+item.getcatagorycname());
			}else{
				if(temp != null){
					vteacherinfolistInfoDTOArray.add(temp);
				}
				temp = new VteacherinfolistInfoDTO();
				temp.setteacherid(item.getteacherid());
				temp.setcatagorycname(item.getcatagorycname());
				temp.setname(item.getname());
				temp.setusername(item.getusername());
				a=item.getteacherid();
			}
		}
		if(temp != null){
			vteacherinfolistInfoDTOArray.add(temp);
		}
		return vteacherinfolistInfoDTOArray;
	}

	
}
